package com.zcah.mask.core;

import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 根据脱敏规则，调用相应的脱敏方法，采用反射启动相应的脱敏任务
 * 
 * @author hy
 *
 */
public class MaskTask {

    /**
     * 需处理的数据
     */
    private String data;
    /**
     * 脱敏算法英文的名字对应代码中的function
     */
    private String ruleName;
    /**
     * 处理的起始位置
     */
    private String beginnum;
    /**
     * 处理的总位数
     */
    private String size;

    /**
     * 创建MaskTask的日志
     */
    private final static Logger logger = LoggerFactory.getLogger(MaskTask.class);
    
    

    /**
     * 无参构造方法
     */
    public MaskTask() {
        super();
    }

    /**
     * 有参的构造方法
     * 
     * @param data
     * @param ruleName
     * @param beginnum
     * @param size
     */
    public MaskTask(String data, String ruleName, String beginnum, String size) {
        this.data = data;
        this.ruleName = ruleName;
        this.beginnum = beginnum;
        this.size = size;
    }

    /**
     * 设置数据
     * 
     * @param data
     */
    public void setData(String data) {
        this.data = data;
    }

    /**
     * 获取数据
     * 
     * @return String类型 data
     */
    public String getData() {
        return this.data;
    }

    /**
     * 设置规则名
     * 
     * @param ruleName
     */
    public void setRuleName(String ruleName) {
        this.ruleName = ruleName;
    }

    /**
     * 获取规则名
     * 
     * @return String类型 ruleName
     */
    public String getRuleName() {
        return this.ruleName;
    }

    /**
     * 设置起始位置
     * 
     * @param beginnum
     */
    public void setBeginNum(String beginnum) {
        this.beginnum = beginnum;
    }

    /**
     * 获取起始位置
     * 
     * @return String类型 beginNum
     */
    public String getBeginNum() {
        return this.beginnum;
    }

    /**
     * 设置处理的总位数
     * 
     * @param size
     */
    public void setSize(String size) {
        this.size = size;
    }

    /**
     * 获取处理的总位数
     * 
     * @return String类型
     */
    public String getSize() {
        return this.size;
    }

    /**
     * 根据脱敏规则，调用相应的脱敏方法，采用反射实现
     * 
     * @return String 类型的返回，脱敏的结果
     */
    public String maskRuleTask() {
        String res;
        Object[] params = { this.getData(), this.getBeginNum(), this.getSize() };
        Class<?>[] inputClass = { String.class, String.class, String.class };
        Class<?> clazz = MaskMethod.class;
        // 调用脱敏方法进行脱敏
        try {
            Method method = clazz.getMethod(this.ruleName, inputClass);
            Object o = clazz.newInstance();
            res = (String) method.invoke(o, params);
            return res;
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage(), e);
            return "";
        }
    }

    @Override
    public String toString() {
        return "MaskTask{" +
                "data='" + data + '\'' +
                ", ruleName='" + ruleName + '\'' +
                ", beginnum='" + beginnum + '\'' +
                ", size='" + size + '\'' +
                '}';
    }
}
